import 'package:flutter/material.dart';

void main() => runApp(MsgEchoApp());

class MsgEchoApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '回音',
      home: MsgListPage(),
    );
  }
}

// 消息展示页面容器
class MsgListPage extends StatelessWidget {
  final msgListKey = GlobalKey<_MsgListWidgetState>(debugLabel: 'msgListKey');

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('消息列表'),
      ),
      body: MsgListWidget(
        key: msgListKey,
      ),
      floatingActionButton: FloatingActionButton(
        tooltip: 'add msg',
        child: Icon(Icons.add),
        onPressed: () async {
          final res = await Navigator.push(
              context, MaterialPageRoute(builder: (_) => AddMsgPage()));
          if (res is Msg) {
            msgListKey.currentState.addMsg(res);
          }
        },
      ),
    );
  }
}

// 消息列表展示
class MsgListWidget extends StatefulWidget {
  MsgListWidget({Key key}) : super(key: key);

  @override
  _MsgListWidgetState createState() => _MsgListWidgetState();
}

class _MsgListWidgetState extends State<MsgListWidget> {
  List<Msg> msgList = [];

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemBuilder: (context, index) {
        final msg = msgList[index];
        final subTitle = DateTime.fromMillisecondsSinceEpoch(msg.timestamp)
            .toLocal()
            .toIso8601String();
        return ListTile(
          title: Text(msg.msg),
          subtitle: Text(subTitle),
        );
      },
      itemCount: msgList.length,
    );
  }

  addMsg(Msg msg) {
    setState(() {
      msgList.add(msg);
    });
  }
}

// 添加消息页面
class AddMsgPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('添加消息'),
      ),
      body: FormWidget(),
    );
  }
}

class FormWidget extends StatefulWidget {
  @override
  _FormWidgetState createState() => _FormWidgetState();
}

class _FormWidgetState extends State<FormWidget> {
  var textEditingController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(16),
      child: Row(
        children: [
          Expanded(
              child: Container(
            margin: EdgeInsets.only(right: 8.0),
            child: TextField(
              controller: textEditingController,
              autofocus: true,
              decoration: InputDecoration(
                  hintText: 'input msg', contentPadding: EdgeInsets.all(0.0)),
              style: TextStyle(color: Colors.black45, fontSize: 22.0),
            ),
          )),
          InkWell(
            onTap: () {
              final msg = Msg(textEditingController.text,
                  DateTime.now().millisecondsSinceEpoch);
              Navigator.pop(context, msg);
            },
            child: Container(
              padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 16.0),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(5.0),
                  color: Colors.black12),
              child: Text('send'),
            ),
          )
        ],
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    textEditingController.dispose();
  }
}

// 实体
class Msg {
  final String msg;
  final int timestamp;

  Msg(this.msg, this.timestamp);

  @override
  String toString() {
    return 'Msg{msg: $msg, timestamp: $timestamp}';
  }
}
